﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using octopus_project.screen;

namespace octopus_project.scene {

    class Timer_GameplayEntry : GameplayEntry {

        private double startCount;
        private double endCount;
        private TimeSpan timer;
        private DateTime inicio;
        public String displayValue { get; private set; }
        public Boolean isActive { get; private set; }
        public Boolean isComplete { get; private set; }
        private int seconds =  30;
        private Vector2 FontOrigin;
        private Vector2 position;

        SpriteFont spriteFont;

        public Timer_GameplayEntry(GameScreen gameScreen, Vector2 position, int paramSeconds = 15) : base(gameScreen) {
            this.isActive = false;
            this.isComplete = false;
            this.displayValue = "None";
            inicio = DateTime.Now;
            timer = TimeSpan.Zero;
            seconds = paramSeconds;
            spriteFont = ScreenManager.Game.Content.Load<SpriteFont>("fonts/timer");
            this.position = position;
        }

        public override void Update () {
            
            if(isActive == false) {
                set(seconds);
            }
            else {
                checkTimer();
            }
  
            
        }

        public override void Draw () {
            spriteBatch.Begin();
            FontOrigin = spriteFont.MeasureString(displayValue) / 2;
            FontOrigin.X += position.X;
            FontOrigin.Y += position.Y;
            
            if(isActive == true) {
                if(endCount > 10) {
                    spriteBatch.DrawString(spriteFont, displayValue, position, Color.White);
                }
                else {
                    spriteBatch.DrawString(spriteFont, displayValue, position, Color.Pink);
                }
            }
            spriteBatch.End();  

            
        }

        public void set(double seconds) {
            this.startCount = timer.TotalSeconds;
            this.endCount = this.startCount + seconds;
            this.isActive = true;
            this.displayValue = this.endCount.ToString();
        }

        public Boolean checkTimer() {
            if (this.isComplete == false) {
                timer = DateTime.Now - inicio;
                if (timer.TotalSeconds > this.startCount + 1) {
                    this.startCount = timer.TotalSeconds;
                    this.endCount = this.endCount - 1;
                    this.displayValue = this.endCount.ToString();

                    if (this.endCount < 10) {
                        this.displayValue = "0" + this.displayValue;
                    }
                    
                    if (this.endCount < 0) {
                        this.endCount = 0;
                        this.isComplete = true;
                        this.displayValue = "0";
                    }
                }
            }
            else {
                this.displayValue = "0";
            }
            return this.isComplete;
        }

        public void reset () {
            this.isActive = false;
            this.isComplete = false;
            this.displayValue = "None";
            //this.startCount = 0;
            this.endCount = 0;
        }
    }  
}
